"""
脚本:读取H5文件中的网格数据并使用PyVista进行可视化

运行:
    python .\Viewer.py -i D:\FE3d_dev\FE3d\test\test_truss_static\results.h5 -v Umag
    
说明:
    -i: 指定H5文件的路径
    -v: 指定要可视化的变量名称
"""

import numpy as np
import pyvista as pv
import h5py,sys
import argparse

parser=argparse.ArgumentParser(description="PyVista Viewer Example")
# # 添加位置参数
parser.add_argument('-i', type=str, required=True, help='Path to the H5 file containing mesh data')
parser.add_argument('-v', type=str, required=True, help='Name of the variable to be visualized')
# 解析命令行参数
args = parser.parse_args()
h5_file :str = 'D:/FE3d_dev/FE3d/test/test_truss_static/results.h5'
print(f"Loading mesh from {h5_file}...")
with h5py.File(h5_file, 'r') as odb:
    scarlar=odb['Result'][args.v][:] # type: ignore
    cell=odb['Cells'][:] # type: ignore
    cell_type=odb['Cell_types'][:] # type: ignore
    node=odb['Nodes'][:] # type: ignore
    # 创建网格
    mesh=pv.UnstructuredGrid(cell, cell_type, node)
    plotter = pv.Plotter() # 创建绘图器对象
    plotter.add_mesh(mesh,scalars=scarlar, show_edges=True, scalar_bar_args={'title': args.v, 'title_font_size': 16,
                                                                            'label_font_size': 14})
    plotter.show()
    # input("Press any key to continue...")  # 暂停脚本,等待用户操作
